package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.UserService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@Slf4j
@RequestMapping("/user/springsession")
public class UserSpringSessionController {
    @Autowired
private UserService userService;

//用户登录
@ResponseBody
@RequestMapping(value="login.do")
@ApiOperation(
        value = "login.do",
        notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions",
        response = ServerResponse.class)

public ServerResponse<User> login(@RequestParam("username") String username, @RequestParam("password")String password, HttpSession session, HttpServletResponse response){

    ServerResponse serverResponse = userService.login(username,password,session);
    if(serverResponse.getStatus()== ResponseCode.SUCCESS.getStatus()){
        session.setAttribute(Const.CURRENT_USER,serverResponse.getData());
    }

    return serverResponse ;
}

@ResponseBody
@RequestMapping("logout.do")
public ServerResponse  logout(HttpSession session){
    session.removeAttribute(Const.CURRENT_USER);
    return ServerResponse.createServerResponseBySuccess();

}

    @ResponseBody
    @ApiOperation(nickname = "swagger-helloworld", value = "Swagger的世界", notes = "测试HelloWorld")
    @RequestMapping("getUserInfo.do")
    public ServerResponse getUserInfoById(HttpServletRequest request, HttpSession session){
    int i = 100;
    int j = 0;
        System.out.println(i/j);
    log.info("8080");
     User user = (User) session.getAttribute(Const.CURRENT_USER);
     return ServerResponse.createServerResponseBySuccess(user);
    }

















}
